﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;


namespace BodyTrain
{
    /// <summary>
    /// LogIn.xaml 的交互逻辑
    /// </summary>
    public partial class LogIn : Window
    {
        public LogIn()
        {
            InitializeComponent();
        }

        private void exit_Click(object sender, RoutedEventArgs e)
        {
            Environment.Exit(0);// 强制退出，即使有其他的线程没有结束
        }

        //用户登录,
        private void logIn_Click(object sender, RoutedEventArgs e)
        {
            MySQL_Service mySQL = new MySQL_Service();
            mySQL.ConnectSer();

            if ((name.Text == string.Empty) || (pwd.Password == string.Empty))
            {
                MessageBox.Show("用户名或密码为空");
            }
            else if ((name.Text == "test"))  //测试用户，供测试使用
            {
                if (pwd.Password == "test")
                {
                    Application.Current.Properties["userName"] = "test";
                    Application.Current.Properties["relation"] = "test";
                    //MessageBox.Show("欢迎测试!");

                    //this.Visibility = Visibility.Hidden;    //隐藏界面
                    MainWindow mainwindow = new MainWindow();
                    mainwindow.Show();
                    //BalanceTrain balanceTrain = new BalanceTrain();
                    //balanceTrain.Show();
                    this.Close();   //关闭登录界面
                }
                else
                    MessageBox.Show("测试用户密码错误");
            }
            else if (comboBox.SelectedIndex < 0)
            {
                MessageBox.Show("未选择家庭成员");
            }
            else if ((pwd.Password != string.Empty) && (name.Text != string.Empty) && (comboBox.SelectedIndex >= 0))  //有用户名和密码输入，并且选择了成员
            {
                int i = mySQL.LogIn(name.Text, pwd.Password, Application.Current.Properties["relation"].ToString());
                //int i= mySQL.LogIn("yan", "123456", "father");
                if (i == 0)
                {
                    Application.Current.Properties["userName"] = name.Text;

                    //this.Visibility = Visibility.Hidden;    //隐藏界面
                    MainWindow mainwindow = new MainWindow();
                    mainwindow.Show();
                    this.Close();   //关闭登录界面

                }
                else if (i == 1)
                    MessageBox.Show("用户名或密码错误");
                else if (i == 2)
                    MessageBox.Show("成员选择错误");
            }

            mySQL.CloseServer();
        }

        //选择成员，并将成员信息赋值给全局变量 relation
        private void comboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //索引是从0开始   男主人:host，女主人:hostess，父亲:father 母亲:mother 儿子:son 女儿:daughter
            switch (comboBox.SelectedIndex)
            {
                case 0:
                    Application.Current.Properties["relation"] = "host";
                    break;
                case 1:
                    Application.Current.Properties["relation"] = "hostess";
                    break;
                case 2:
                    Application.Current.Properties["relation"] = "father";
                    break;
                case 3:
                    Application.Current.Properties["relation"] = "mother";
                    break;
                case 4:
                    Application.Current.Properties["relation"] = "son";
                    break;
                case 5:
                    Application.Current.Properties["relation"] = "daughter";
                    break;
            }
        }


    }
}
